package cn.hc.mq.delay3;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/9/19 19:00
 */
public class DelayProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("pg");
        producer.setNamesrvAddr("rocketmqOS1:9876;rocketmqOS2:9876");
        producer.start();

        for (int i = 0; i < 100; i++) {
            byte[] body = ("Hi," + i).getBytes();
            Message msg = new Message("delayTopic", "delayTag", body);
            msg.setDelayTimeLevel(3);
            SendResult sendResult = producer.send(msg);
            System.out.print(new SimpleDateFormat("mm:ss").format(new Date()));
            System.out.println("," + sendResult);
        }
        producer.shutdown();
    }
}
